package com.example.flatlafdemo;

import com.example.flatlafdemo.components.mainPanel.MainPanel;
import com.example.flatlafdemo.components.menubar.FlatlafMenuBar;
import com.example.flatlafdemo.utils.IconUtil;
import com.formdev.flatlaf.FlatLightLaf;
import org.jdesktop.swingx.JXStatusBar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

@SpringBootApplication
public class FlatlafDemoApplication extends JFrame {

    public static FlatlafDemoApplication frame;

    private MainPanel mainPanel;
    private JXStatusBar statusBar;

    public FlatlafDemoApplication() {
        super("Flatlaf Demo");
        try {
            UIManager.setLookAndFeel(new FlatLightLaf());

            this.frame = this;

            initFrame();

            setIconImage(IconUtil.Frame_Icon.getImage());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setMinimumSize(new Dimension(800, 600));
            setLocationRelativeTo(null);
            setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "窗口初始化失败!");
        }
    }

    public void updateMainContentPanel(Container container) {
        if (mainPanel != null) {
            mainPanel.updateContentPanel(container);
        }
    }

    private void initFrame() {
        setJMenuBar(new FlatlafMenuBar());

        mainPanel = new MainPanel();
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        statusBar = new JXStatusBar();
        getContentPane().add(statusBar, BorderLayout.SOUTH);
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            int option = JOptionPane.showConfirmDialog(this, "确定要退出吗?",
                    "Flatlaf Demo", JOptionPane.YES_NO_OPTION);
            if (option != JOptionPane.YES_OPTION) {
                return;
            }
        }
        super.processWindowEvent(e);
    }

    public static void main(String[] args) {
        SpringApplication.run(FlatlafDemoApplication.class, args);
    }

}
